/**
 * @author hanzc
 * @date 2021-01-22
 */
import http from './http';
import constant from './constant';

// 查询select选项
export const querySelectOptions = params =>
    http.post(constant.GENERAL_VIEW_REPORT.QUERY_SELECT_OPTIONS, params);

// 查询dashboard数据, 包括card和chart
export const queryPart1Values = params =>
    http.post(constant.GENERAL_VIEW_REPORT.QUERY_PART1_VALUES, params);

export const queryPart2Values = params =>
    http.post(constant.GENERAL_VIEW_REPORT.QUERY_PART2_VALUES, params);

// 查询card和chart的详情
export const queryDetailInPart1 = params =>
    http.post(constant.GENERAL_VIEW_REPORT.QUERY_DETAIL_IN_PART1, params);

export const queryDetailInPart2 = params =>
    http.post(constant.GENERAL_VIEW_REPORT.QUERY_DETAIL_IN_PART2, params);

// 下载card和chart
const downloadByPost = (url, params, excelName = '') => {
    http({
        url,
        method: 'post',
        data: {
            ...params,
        },
        responseType: 'blob',
    }).then(res => {
        const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;',
        });
        const downloadElement = document.createElement('a');
        const href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        const time = new Date().getTime();
        downloadElement.download = `${excelName}${time}.xlsx`;
        // document.body.appendChild(downloadElement);
        downloadElement.click();
        // document.body.removeChild(downloadElement);
        // window.URL.revokeObjectURL(href);
    });
};

export const downloadInPart1 = (downloadParams = {}) => {
    const {params = {}, excelName = ''} = downloadParams;
    downloadByPost(
        constant.GENERAL_VIEW_REPORT.DOWNLOAD_IN_PART1,
        params,
        excelName
    );
};

export const downloadInPart2 = (downloadParams = {}) => {
    const {params = {}, excelName = ''} = downloadParams;
    downloadByPost(
        constant.GENERAL_VIEW_REPORT.DOWNLOAD_IN_PART2,
        params,
        excelName
    );
};
